package logic

import (
	"context"
	"encoding/json"
	"pet/models/cache"
	"pet/models/mysql"
	"pet/petrpc/internal/svc"
	"pet/petrpc/petrpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type PetListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPetListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PetListLogic {
	return &PetListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PetListLogic) PetList(in *petrpc.PetListRequest) (*petrpc.PetListResponse, error) {
	// todo: add your logic here and delete this line

	key := "petList"
	var pets []mysql.Pet
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		mysql.DB.Debug().Find(&pets)
		Jsondata, _ := json.Marshal(pets)
		cache.Rdb.Set(cache.Ctx, key, Jsondata, time.Hour*1)
	} else {
		json.Unmarshal([]byte(cacheData), &pets)
	}

	return &petrpc.PetListResponse{
		List: nil,
	}, nil
}
